﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ue.Eagle.Runtime.Models;
using Ue.Eagle.Runtime.FileSystems;
using Ue.Extensions;
using Ue.Core.FileSystems;

namespace Ue.Eagle.Runtime.Folders
{
    public class PluginFolder : IPluginFolder
    {
        private readonly IEnumerable<string> paths;
        private readonly string manifestName;
        private readonly IPluginHarvester harvester;
        private readonly IFolder folder;

        public PluginFolder(IEnumerable<string> paths, string manifestName, IFolder folder, IPluginHarvester harvester)
        {
            this.paths = paths;
            this.harvester = harvester;
            this.folder = folder;
            this.manifestName = manifestName;
        }

        #region IPluginFolder 成员

        public IEnumerable<PluginDescriptor> AvailablePlugins()
        {
            return this.paths
                .SelectMany(path => folder.GetDirectories(path, true))
                .Select(dictionary => this.harvester.HarvestPlugin(dictionary, this.manifestName))
                .Where(plugin => plugin != null);
        }

        #endregion
    }
}
